unit Unit27; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, sqldb, db,
  DBGrids, Unit1;

type

  { TForm27 }

  TForm27 = class(TForm)
    Datasource1: TDatasource;
    DBGrid1: TDBGrid;
    SQLQuery1: TSQLQuery;
    procedure DBGrid1CellClick(Column: TColumn);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow(Sender: TObject);
  private
    { private declarations }
  public
    codigo_producto : string;
  end; 

var
  Form27: TForm27; 

implementation

{ TForm27 }

procedure TForm27.FormShow(Sender: TObject);
var
  producto : String;
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;

  codigo_producto := '';
    
  producto := InputBox('Búsqueda de código de producto', 'Ingrese parte del nombre del producto:', '');
  
  if producto <> '' then
  begin
    SQLQuery1.DataBase := Form1.PQConnection1;
    SQLQuery1.Transaction := Form1.SQLTransaction1;
    SQLQuery1.SQL.Text := 'select prd_cdg, prd_nombre, prd_descripcion, fmp_nombre,'
      + ' fab_nombre from vw_producto where prd_nombre ilike ''%' + producto + '%''';
    
    SQLQuery1.Open;

    DBGrid1.Columns[0].Title.Caption := 'Codigo';
    DBGrid1.Columns[1].Title.Caption := 'Nombre';
    DBGrid1.Columns[2].Title.Caption := 'Descripcion';
    DBGrid1.Columns[3].Title.Caption := 'Familia';
    DBGrid1.Columns[4].Title.Caption := 'Fabricante';

    DBGrid1.Columns[0].Width := 60;
    DBGrid1.Columns[1].Width := 150;
    DBGrid1.Columns[2].Width := 130;
    DBGrid1.Columns[3].Width := 100;
    DBGrid1.Columns[4].Width := 120;
  end
  else
    Close;
end;

procedure TForm27.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
end;

procedure TForm27.DBGrid1CellClick(Column: TColumn);
begin
  codigo_producto := SQLQuery1.FieldByName('prd_cdg').AsString;
  Close;
end;

initialization
  {$I unit27.lrs}

end.

